package collection.map2.treemap;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import org.junit.Test;

public class TreeMapDemo {

	@Test
	public void testSort() {
		// 默认按key升序排列
		Map<Object, Object> map = new TreeMap<Object, Object>();
		map.put("M", "A");
		map.put("D", "S");
		map.put("E", "E");
		map.put("A", "W");
		System.out.println(map);
	}

	@Test
	public void testSort2() {
		// 使用Comparator构造 按key降序排列
		Map<String, String> map = new TreeMap<String, String>(
				new Comparator<String>() {

					@Override
					public int compare(String o1, String o2) {
						return o2.compareTo(o1);
					}

				});
		map.put("M", "A");
		map.put("D", "S");
		map.put("E", "E");
		map.put("A", "W");
		System.out.println(map);
	}

	@Test
	public void testPut() {
		// value可以存在多个null
		Map<Object, Object> map = new TreeMap<Object, Object>();
		map.put("M", "A");
		map.put("E", null);
		map.put("D", null);
		System.out.println(map);
	}

	@Test
	public void testPut2() {
		// key不能为null
		Map<Object, Object> map = new TreeMap<Object, Object>();
		map.put("M", "A");
		map.put(null, "E");
		System.out.println(map);
	}

}
